/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.xmlparser.listening;

import com.edu.kamai.xml.beans.paperXML;
import com.edu.kamai.xml.beans.choiceXML;
import com.edu.kamai.xml.beans.listening.listeningQuestion;
import com.edu.kamai.xml.beans.listening.listeningXML;
import com.edu.kamai.xml.beans.reading.readingXML;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author xyzhu
 */
public class listeningPaperParser {
    
    public listeningPaperParser(String fn){
        fileName = fn;
    }
    
    public listeningXML parseSpeakingXML(){
        paperXML pxml = new readingXML();
        try {
            File xmlFile = new File(fileName);
            if(xmlFile.exists()){
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(xmlFile);
                doc.getDocumentElement().normalize();
                
                int id = Integer.parseInt(doc.getDocumentElement().getAttribute("paperRoleID"));
                pxml.setPaperRoleID(id);
                
                NodeList nList = doc.getElementsByTagName("readingQestion");
                
                for (int i = 0; i < nList.getLength(); i++) {
                    Node nNode = nList.item(i);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        listeningQuestion lq = new listeningQuestion();
                        String MainTxt = getTagValue("MainTxt", eElement);
                        String Title = getTagValue("Title", eElement);
                        String Desc = getTagValue("Desc", eElement);
                        String mp3path = getTagValue("mp3path", eElement);
                        //System.out.println(MainTxt);
                        int timeLimit = Integer.parseInt(
                                getTagValue("TimeLimit", eElement));
                        
                        lq.setMainTxt(MainTxt);
                        lq.setTitle(Title);
                        lq.setDescription(Desc);
                        lq.setMP3Path(mp3path);
                        lq.setTimeLimit(timeLimit);
                        
                        NodeList choiceList = eElement.getElementsByTagName("choice");
                        for(int j=0;j<choiceList.getLength();j++){
                            Node choiceNode = choiceList.item(j);
                            choiceXML cxml = new choiceXML(false);
                            
                            Element choiceElement = (Element)choiceNode;
                            String choiceMainTxt = getTagValue("ChoiseMainTxt", choiceElement);
                            String choiceKey = getTagValue("ChoiseKey", choiceElement);
                            cxml.setMainTxt(choiceMainTxt);
                            cxml.addOneKey(choiceKey);
                            System.out.println(choiceMainTxt + " " + choiceKey);
                            
                            NodeList optionList = choiceElement.getElementsByTagName("optionTxt");
                            for(int k=0;k<optionList.getLength();k++){
                                Node optionNode = optionList.item(k);
                                String optionTxt = optionNode.getTextContent();
                                System.out.println(optionTxt);
                                cxml.addOneChoiceTxt(optionTxt);
                            }
                            lq.addChoices(cxml);
                        }
                        pxml.addListeningQuestionToPaper(lq);
                    }
                }
                
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        
        return (listeningXML)pxml;
    }
    
    
    private static String getTagValue(String sTag, Element eElement) {
	NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        if(nValue == null)return "";
        String retValue = nValue.getNodeValue();
        return retValue;
    }
    
    
    
    private String fileName;
    
    public static void main(String args[]){
        /*
        String filename = "C:\\Users\\user\\Documents\\NetBeansProjects\\"
                + "trunk\\kamai_edu\\build\\web\\UpLoad\\Paper\\"
                + "f95cee52b884358d4b8bef49a5ec2508.xml";
        listeningPaperParser rpp = new listeningPaperParser(filename);
        rpp.parseSpeakingXML();
         * * 
         */
         
    }
    
}
